package uutalk.netcloud.talk.app;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.telephony.TelephonyManager;

import com.apkfuns.logutils.LogUtils;

import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.app.ui.Activity.ChooseLanuageActivity;
import uutalk.netcloud.talk.app.ui.MainActivity;
import uutalk.netcloud.talk.app.ui.MainActivityBak;
import uutalk.netcloud.talk.base.android.BaseActivity;
import uutalk.netcloud.talk.base.util.LanguageUtil;
import uutalk.netcloud.talk.base.util.PermissionUtils;
import uutalk.netcloud.talk.base.util.SharedPreferencesUtil;
import uutalk.netcloud.talk.base.util.view.ToastUtil;


public class ALauncher extends BaseActivity {

    private final static int REQUEST_PERMISSIONS = 999;
    private final int MSG_ENTER = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.e("启动程序");
        if (!isTaskRoot()) {
            finish();
            return;
        }
        setContentView(R.layout.lz_activity_lauch);
    }


    @Override
    public void init() {
        //一次性把权限申请了
        if (Build.VERSION.SDK_INT >= 23) {
            String[] permissions = new String[6];
            permissions[0] = PermissionUtils.PERMISSION_WRITE_EXTERNAL_STORAGE;
            permissions[1] = PermissionUtils.PERMISSION_READ_EXTERNAL_STORAGE;
            permissions[2] = PermissionUtils.PERMISSION_CAMERA;
            permissions[3] = PermissionUtils.PERMISSION_ACCESS_COARSE_LOCATION;
            permissions[4] = PermissionUtils.PERMISSION_ACCESS_FINE_LOCATION;
            permissions[5] = PermissionUtils.PERMISSION_RECORD_AUDIO;
            boolean hasAllPermission = PermissionUtils.requestPermissions(mActivity, permissions, REQUEST_PERMISSIONS);
            if(hasAllPermission){
                sendMessageToPageDelay(MSG_ENTER, 2000l);
            }
        } else {
            sendMessageToPageDelay(MSG_ENTER, 2000l);
        }
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if(msg.what == MSG_ENTER){
            int launch = SharedPreferencesUtil.getInt(mActivity, Constants.FIRST_LAUNCH, 0);
            if(launch == 0){
                LogUtils.e("goto ChooseLanuageActivity");
                Intent intent = new Intent(mActivity, ChooseLanuageActivity.class);
                startActivity(intent);
            } else {
//                Intent intent = new Intent(mActivity, MainActivity.class);
                LogUtils.e("goto MainActivityBak");
                Intent intent = new Intent(mActivity, MainActivity.class);
                startActivity(intent);
            }
            finish();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @android.support.annotation.NonNull String[] permissions, @android.support.annotation.NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == REQUEST_PERMISSIONS){
            int length = grantResults.length;
            for(int i=0; i<length; i++){
                if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
                    ToastUtil.makeToast(LanguageUtil.getStringById(R.string.permission_lost));
                    break;
                }
            }
            sendMessageToPage(MSG_ENTER);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    public boolean supportActionbar() {
        return false;
    }

    @Override
    public int layoutId() {
        return R.layout.a_launcher_layout;
    }


    @Override
    public void gc() {
        super.gc();
    }

    @Override
    public int getThemeId() {
        return R.style.TranlucentStatusTheme;
    }
}
